<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>das</div>
</body>
<script>
    const pubSub = {
        list: {}, // {'订阅事件1'：[fn1,fn2,....],'订阅事件2':[fn1,fn2,....]}
        subscribe (key, fn) { //订阅
            if (!this.list[key]) {
                this.list[key] = []
            }
            this.list[key].push(fn)
            // console.log("this.list[key]", this.list[key]);
        },
        //发布
        publish (...arguments) {
            const arr = arguments;
            const key = Array.prototype.shift.call(arr)
            // console.log("【key】", key);
            let fns = this.list[key];
            if (!fns || fns.length <= 0) return false;
            const len = fns.length
            console.log("fns", fns);
            for (let i = 0; i < len; i++) {
                // console.log("fns[i]", fns[i]);
                fns[i].apply(this, arr)
            }
        },
        //删除
        unSubscribe (key) {
            delete this.list[key]
        }
    }
    const callBack = (aa) => {
        console.log("aa", aa);
    }
    const callBackSubscribe = (callBackSubscribe) => {
        console.log("callBackSubscribe", callBackSubscribe);
    }
    const callBackOne = () => {
        console.log("publish");
    }
    const callBackTwo = () => {
        console.log("publishcallBackTwo");
    }
    pubSub.subscribe("test", callBack)
    pubSub.subscribe("test", callBackSubscribe)
    pubSub.publish('test', callBackOne)
    pubSub.publish('test', callBackTwo)
</script>

</html>